import { asyncRouterMap, constantRouterMap } from '../config/router.config'
import cloneDeep from 'lodash.clonedeep'
import { defineStore } from 'pinia'
/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
function hasPermission(permission, route) {
    if (route.meta && route.meta.permission) {
        if (permission === undefined) {
            return false
        }
        let flag = false
        for (let i = 0, len = permission.length; i < len; i++) {
            flag = route.meta.permission.includes(permission[i])
            if (flag) {
                return true
            }
        }
        return false
    }
    return true
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole(roles, route) {
    if (route.meta && route.meta.roles) {
        return route.meta.roles.includes(roles.id)
    } else {
        return true
    }
}

function filterAsyncRouter(routerMap, role) {
    const accessedRouters = routerMap.filter(route => {
        if (hasPermission(role.permissionList, route)) {
            if (route.children && route.children.length) {
                route.children = filterAsyncRouter(route.children, role)
            }
            return true
        }
        return false
    })
    return accessedRouters
}

const usePermissionStore = defineStore('permission', {
    state: () => ({
        routers: constantRouterMap,
        addRouters: []
    }),
    actions: {
        SET_ROUTERS(routers) {
            this.addRouters = routers
            this.routers = constantRouterMap.concat(routers)
        },
        GenerateRoutes(data) {
            return new Promise(resolve => {
                const { role } = data
                const routerMap = cloneDeep(asyncRouterMap)
                const accessedRouters = filterAsyncRouter(routerMap, role)
                this.SET_ROUTERS(accessedRouters)
                resolve()
            })
        }
    }
})

export default usePermissionStore
